package com.xky.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xky.mapper.BookMapper;
import com.xky.pojo.Book;
import com.xky.pojo.PageBean;
import com.xky.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    private static final Logger log = LoggerFactory.getLogger(BookServiceImpl.class);

    @Autowired
    private BookMapper bookMapper;
    /**
     * 添加图书信息
     * @param book
     */
    @Override
    public void addBooks(Book book) {
        book.setStatus("未借");
        bookMapper.insert(book);
    }

    /**
     * 删除图书信息
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }

    /**
     * 更新图书信息
     * @param book
     */
    @Override
    public void updateById(Book book) {
        bookMapper.updateById(book);
    }

    /**
     * 批量删除图书信息
     * @param id
     */
    @Override
    public void deleteSelect(List<Integer> id) {
        bookMapper.deleteBatchIds(id);
    }

    /**
     * 根据id查询图书信息
     * @param id
     * @return
     */
    @Override
    public Book selectById(Integer id) {
        return bookMapper.selectById(id);
    }

    /**
     * 查询全部的图书信息
     * @return
     */
    @Override
    public List<Book> selectAll() {
        return bookMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public PageBean select(String bookName, String bookLocation, String bookType, Integer page, Integer pageSize) {
        // 创建分页对象
        IPage<Book> pageParam = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page,pageSize);

        // 构建查询条件
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        if (bookName != null && !bookName.isEmpty()) {
            queryWrapper.like(Book::getBookName, bookName);
        }
        if (bookLocation != null && !bookLocation.isEmpty()) {
            queryWrapper.eq(Book::getBookLocation, bookLocation);
        }
        if (bookType != null && !bookType.isEmpty()) {
            queryWrapper.eq(Book::getBookType, bookType);
        }

        // 执行分页查询
        IPage<Book> result = bookMapper.selectPage(pageParam, queryWrapper);

        // 封装自定义分页对象
        return new PageBean(result.getTotal(), result.getRecords());
    }

    /**
     * 删除编号为?的图书
     * @param bookCode
     */
    @Override
    public void updateByCode(Book book,Integer bookCode) {
        log.info("开始修改图书，编号: {}", bookCode);
       book.setStatus("已借");
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getBookCode, bookCode);
        int rows = bookMapper.update(book,queryWrapper);
        log.info("修改图书完成，影响行数: {}", rows);
        if (rows == 0) {
            throw new RuntimeException("未找到要修改的图书记录");
        }
    }

    @Override
    public Book selectByCode(Integer bookCode) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getBookCode, bookCode);
        return bookMapper.selectOne(queryWrapper);
    }

    @Override
    public void deleteByCode(Integer bookCode) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getBookCode, bookCode);
         bookMapper.delete(queryWrapper);
    }

    @Override
    public void updateByCodes(Book book, Integer bookCode) {
        log.info("开始修改图书，编号: {}", bookCode);
        book.setStatus("未借");
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getBookCode, bookCode);
        int rows = bookMapper.update(book,queryWrapper);
        log.info("修改图书完成，影响行数: {}", rows);
        if (rows == 0) {
            throw new RuntimeException("未找到要修改的图书记录");
        }
    }
}
